package com.example.jichu_1.fragment;


import android.os.Bundle;
import android.view.ContextMenu;
import android.view.LayoutInflater;
import android.view.Menu;
import android.view.MenuInflater;
import android.view.MenuItem;
import android.view.View;
import android.view.ViewGroup;
import android.widget.Toast;

import androidx.annotation.NonNull;
import androidx.fragment.app.Fragment;
import androidx.recyclerview.widget.DividerItemDecoration;
import androidx.recyclerview.widget.LinearLayoutManager;
import androidx.recyclerview.widget.RecyclerView;

import com.example.jichu_1.R;
import com.example.jichu_1.adapter.HomeAdapter;
import com.example.jichu_1.api.ApiService;
import com.example.jichu_1.bean.BanBean;
import com.example.jichu_1.bean.GirlBean;
import com.example.jichu_1.presenter.Presenter;
import com.example.jichu_1.view.IView;

import java.util.List;

import io.reactivex.Observer;
import io.reactivex.android.schedulers.AndroidSchedulers;
import io.reactivex.disposables.Disposable;
import io.reactivex.schedulers.Schedulers;
import retrofit2.Retrofit;
import retrofit2.adapter.rxjava2.RxJava2CallAdapterFactory;
import retrofit2.converter.gson.GsonConverterFactory;

/**
 * A simple {@link Fragment} subclass.
 */
public class HomeFragment extends Fragment implements IView {


    private RecyclerView mRecl;
    private Presenter presenter;
    private HomeAdapter homeAdapter;
    private int pos;

    public HomeFragment() {
        // Required empty public constructor
    }


    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container,
                             Bundle savedInstanceState) {
        // Inflate the layout for this fragment
        View inflate = inflater.inflate(R.layout.fragment_home, container, false);
        initView(inflate);
        presenter = new Presenter(this);
        presenter.getbanners();
        initData();
        return inflate;
    }

    private void initData() {
        new Retrofit.Builder()
                .baseUrl(ApiService.GirlsUrl)
                .addConverterFactory(GsonConverterFactory.create())
                .addCallAdapterFactory(RxJava2CallAdapterFactory.create())
                .build().create(ApiService.class)
                .getGirls()
                .subscribeOn(Schedulers.io())
                .observeOn(AndroidSchedulers.mainThread())
                .subscribe(new Observer<GirlBean>() {
                    @Override
                    public void onSubscribe(Disposable d) {

                    }

                    @Override
                    public void onNext(GirlBean girlBean) {
                        homeAdapter.addList(girlBean.getData());
                    }

                    @Override
                    public void onError(Throwable e) {

                    }

                    @Override
                    public void onComplete() {

                    }
                });
    }

    private void initView(@NonNull final View itemView) {
        mRecl = (RecyclerView) itemView.findViewById(R.id.recl);
        mRecl.setLayoutManager(new LinearLayoutManager(getContext()));
        mRecl.addItemDecoration(new DividerItemDecoration(getContext(),DividerItemDecoration.VERTICAL));
        setHasOptionsMenu(true);
        homeAdapter = new HomeAdapter(getContext());
        mRecl.setAdapter(homeAdapter);
        registerForContextMenu(mRecl);
        homeAdapter.setOnMyItemClick(new HomeAdapter.OnMyItemClick() {


            @Override
            public void MyItemClick(int position) {
                pos = position;
            }
        });
    }

    @Override
    public void onCreateContextMenu(ContextMenu menu, View v, ContextMenu.ContextMenuInfo menuInfo) {
        menu.add(0, 0, 0, "添加");
//        menu.add(0, 1, 0, "分享");
        super.onCreateContextMenu(menu, v, menuInfo);
    }

    @Override
    public boolean onContextItemSelected(@NonNull MenuItem item) {
        switch (item.getItemId()) {
            case 0:
                break;

        }
        return super.onContextItemSelected(item);

    }

    @Override
    public void success(List<BanBean.DataBean> banners) {
        homeAdapter.addBanners(banners);
    }

    @Override
    public void failed(String error) {
        Toast.makeText(getContext(), ""+error, Toast.LENGTH_SHORT).show();
    }

    @Override
    public void onCreateOptionsMenu(@NonNull Menu menu, @NonNull MenuInflater inflater) {
        menu.add(0,0,0,"搜索");
        menu.add(0,1,0,"分享");
        super.onCreateOptionsMenu(menu, inflater);
    }

    @Override
    public boolean onOptionsItemSelected(@NonNull MenuItem item) {
        switch (item.getItemId()){
            case 0:
            Toast.makeText(getContext(), ""+item.getTitle(), Toast.LENGTH_SHORT).show();
            break;
            case 1:
                Toast.makeText(getContext(), "" + item.getTitle(), Toast.LENGTH_SHORT).show();
                break;
        }
        return super.onOptionsItemSelected(item);
    }
}
